#include "gplib_print_string.h"

#if (GPLIB_PRINT_STRING_EN == 2) && defined(C_DEBUG_MESSAGE) && C_DEBUG_MESSAGE && defined(DBG_PRINT_UART)
#include <stdarg.h>
#include <stdio.h>

#if (_DRV_L1_UART == 1) && defined(DBG_PRINT_UART)
#define SEND_DATA(x)	uart_data_send(DBG_PRINT_UART, x, 1)
#define GET_DATA(x)		uart_data_get(DBG_PRINT_UART, x, 1)
#else
#define SEND_DATA(x)
#define GET_DATA(x) (*(x) = '\r')
#endif
static CHAR print_buf[PRINT_BUF_SIZE];
static INT8S uart_tx_buf[PRINT_BUF_SIZE];

void print_string(const CHAR *const fmt, ...)
{
	va_list v_list;
	CHAR	*pt;
	INT32U print_size;

	if( uart_tx_isr_status(DBG_PRINT_UART)!=0 )
	{
		return;
	}

	va_start(v_list, fmt);
	vsprintf(print_buf, fmt, v_list);
	va_end(v_list);

	print_buf[PRINT_BUF_SIZE - 1] = 0;
	pt = print_buf;
	print_size = 0;
	while(*pt)
	{
		uart_tx_buf[print_size++] = *pt;
		pt++;
	}
	uart_tx_isr_set(DBG_PRINT_UART, uart_tx_buf, print_size);
	uart_tx_isr_send(DBG_PRINT_UART);
}

void get_string(CHAR *s)
{
	INT8U	temp;

	while(1)
	{
		GET_DATA(&temp);
		SEND_DATA(temp);
		if(temp == '\r')
		{
			*s = 0;
			return;
		}

		*s++ = (CHAR) temp;
	}
}

#elif (GPLIB_PRINT_STRING_EN == 1) && defined(C_DEBUG_MESSAGE) && C_DEBUG_MESSAGE && defined(DBG_PRINT_UART)
#include <stdarg.h>
#include <stdio.h>

#if (_DRV_L1_UART == 1)
#define SEND_DATA(x)	uart_data_send(DBG_PRINT_UART, x, 1)
#define GET_DATA(x)		uart_data_get(DBG_PRINT_UART, x, 1)
#else
#define SEND_DATA(x)
#define GET_DATA(x) (*(x) = '\r')
#endif
static CHAR print_buf[PRINT_BUF_SIZE];

void print_string(const CHAR *const fmt, ...)
{
	va_list v_list;
	CHAR	*pt;

	va_start(v_list, fmt);
	vsprintf(print_buf, fmt, v_list);
	va_end(v_list);

	print_buf[PRINT_BUF_SIZE - 1] = 0;
	pt = print_buf;
	while(*pt)
	{
		SEND_DATA(*pt);
		pt++;
	}
}

void get_string(CHAR *s)
{
	INT8U	temp;

	while(1)
	{
		GET_DATA(&temp);
		SEND_DATA(temp);
		if(temp == '\r')
		{
			*s = 0;
			return;
		}

		*s++ = (CHAR) temp;
	}
}

#else
void print_string(const CHAR *const fmt, ...)
{ }

void get_string(CHAR *s)
{ }

#endif // GPLIB_PRINT_STRING_EN
